﻿using System;
using System.Collections;
using System.Configuration;
using System.Web;

namespace Utility
{
    public class Cookie
    {
        #region 获取Cookie作用域
        /// <summary>
        /// 获取Cookie作用域
        /// </summary>
        /// <returns></returns>
        public static string GetCookieDoMain()
        {
            string DoMain = "";
            string Url=HttpContext.Current.Request.Url.Host;
            string[] Arr = Url.Split('.');
            if (Url.Contains("localhost")) { return ""; }
            for (int i = 1; i < Arr.Length; i++)
            {
                DoMain += "." + Arr[i];
            }
            return DoMain;
        }
        #endregion

        #region 静态方法：保存Cookie
        /// <summary>
        /// 静态方法：保存Cookie
        /// </summary>
        ///<param name="Hash">哈希表的Cookie序列</param>
        ///<param name="_remberme">有效期时间秒数 1s为永久</param>
        ///<param name="_CookieName">Cookie名称</param>
        public static void SaveCookie(Hashtable Hash, int _RemberSeconds, string _CookieName)
        {
            if (string.IsNullOrEmpty(_CookieName)) { _CookieName = "Web"; }
            HttpCookie Cookie = new HttpCookie(_CookieName);
            foreach (DictionaryEntry entry in Hash)
            {
                Cookie[HttpContext.Current.Server.UrlEncode(entry.Key.ToString())] = HttpContext.Current.Server.UrlEncode(entry.Value.ToString());
            }
            if (_RemberSeconds == 1) { Cookie.Expires = DateTime.MaxValue; } else if (_RemberSeconds > 1) { Cookie.Expires = DateTime.Now.AddSeconds(_RemberSeconds); }
            string DoMain=Utility.Cookie.GetCookieDoMain();
            if (!string.IsNullOrEmpty(DoMain)) { Cookie.Domain = DoMain; }
            HttpContext.Current.Response.Cookies.Add(Cookie);
        }
        /// <summary>
        /// 保存Cookie
        /// </summary>
        /// <param name="CookieValueStr">Cookie参数字符串</param>
        /// <param name="_CookieName">Cookie名称</param>
        /// <param name="_RemberSeconds">过期秒数 1s为永不过期</param>
        public static void SaveCookie(string CookieValueStr, string _CookieName, int _RemberSeconds)
        {
            if (string.IsNullOrEmpty(_CookieName)) { _CookieName = "Web"; }
            HttpCookie Cookie = new HttpCookie(_CookieName);
            Cookie.Value = CookieValueStr;
            if (_RemberSeconds == 1) { Cookie.Expires = DateTime.MaxValue; } else if(_RemberSeconds>1) { Cookie.Expires = DateTime.Now.AddSeconds(_RemberSeconds); }
            string DoMain = Utility.Cookie.GetCookieDoMain();
            if (!string.IsNullOrEmpty(DoMain)) { Cookie.Domain = DoMain; }
            HttpContext.Current.Response.Cookies.Add(Cookie);
        }
        #endregion

        #region 静态方法：读取Cookie的值
        /// <summary>
        /// 静态方法：读取Cookie的值
        /// </summary>
        /// <param name="_name"></param>
        /// <returns></returns>
        public static string GetCookie(string _name, string _CookieName)
        {
            if (string.IsNullOrEmpty(_CookieName)) { _CookieName = "Web"; }
            HttpCookie Cookie = HttpContext.Current.Request.Cookies.Get(_CookieName);
            if (Cookie == null) { return ""; }
            return HttpContext.Current.Server.UrlDecode(Cookie[_name]);
        }
        #endregion

        #region 静态方法：删除Cookie
        /// <summary>
        /// 静态方法：删除Cookie
        /// </summary>
        /// <param name="_CookieName"></param>
        public static void DelCookie(string _CookieName)
        {
            if (string.IsNullOrEmpty(_CookieName)) { _CookieName = "Web"; }
            HttpCookie Cookie = new HttpCookie(_CookieName);
            string DoMain = Utility.Cookie.GetCookieDoMain();
            if (!string.IsNullOrEmpty(DoMain)) { Cookie.Domain = DoMain; }
            Cookie.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.Cookies.Add(Cookie);
        }
        #endregion

    }
}
